<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>发票管理</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link href="{__STATIC__}/layuiAdmin/layui/css/layui.css" rel="stylesheet">
	<link href="{__STATIC__}/layuiAdmin/adminui/dist/css/admin.css" rel="stylesheet">
	<link href="{__STATIC__}/layuiAdmin/style/style.css" rel="stylesheet">
</head>
<body>
	<div class="layui-fluid">
		<div class="layui-card">
			<div class="layui-card-body">
				<div style="padding-bottom: 10px;">
					<div class="layui-table-title">发票管理</div>
					<div class="layui-form search_ctr">
						<div class="layui-form-item">
							<div class="layui-input-inline w160">
								<input type="text" name="k3" id="k3" class="layui-input" placeholder="开始时间" autocomplete="off" value="">
							</div>
							<div class="layui-form-mid layui-word-aux">-</div>
							<div class="layui-input-inline w160">
								<input type="text" name="k4" id="k4" class="layui-input" placeholder="结束时间" autocomplete="off" value="">
							</div>
							<div class="layui-inline">
								<div class="layui-input-inline">
									<input type="text" name="keys" placeholder="输入开票单号/抬头/发票号查询" class="layui-input">
								</div>
								<button class="layui-btn layui-btn-normal" lay-submit lay-filter="LAY-search">查询</button>
							</div>
						</div>
					</div>
				</div>

				<table id="LAY-table" lay-filter="LAY-table"></table>
				<script type="text/html" id="state">
					{{#  if(d.state == 1){ }}
					<span class="layui-badge-dot layui-bg-blue"></span>待开
					{{#  } }}
					{{#  if(d.state == 2){ }}
					<span class="layui-badge-dot layui-bg-green"></span>已开
					{{#  } }}
					{{#  if(d.state == 3){ }}
					<span class="layui-badge-dot layui-bg-gray"></span>驳回
					{{#  } }}
				</script>
				<script type="text/html" id="tableBar">
					<a class="link-text" lay-event="edit">操作</a>
				</script>
			</div>
		</div>
	</div>
</body>
</html>
<script src="{__STATIC__}/layuiAdmin/layui/layui.js"></script>
<script>
	layui.config({
		base: '{__STATIC__}/layuiAdmin/' // 静态资源所在路径
	}).use(['index', 'layer', 'table', 'form', 'laydate'], function(){
		let $ = layui.$;
		let layer = layui.layer;
		let table = layui.table;
		let form = layui.form;
		let laydate = layui.laydate;

		//搜索日期
		laydate.render({
			elem: '#k3'
			,type: 'datetime'
			,trigger: 'click'
		});

		laydate.render({
			elem: '#k4'
			,type: 'datetime'
			,trigger: 'click'
		});

		//初始化列表
		table.render({
			elem: '#LAY-table'
			,url: "{:url('getList')}"
			,cols: [[
				{field: 'id', width: 80, title: 'ID', sort: true}
				,{field: 'sn', width:180, title: '开票单号'}
				,{field: 'tax_title', width: 220, title: '发票抬头'}
				,{field: 'fee', width: 120, title: '开票金额'}
				,{field: 'type', width: 110, title: '发票类型', align:'center'}
				,{field: 'info', minWidth: 200, title: '备注'}
				,{field: 'state', width: 90, title: '状态', align:'center', templet: '#state'}
				,{field: 'add_date', width: 180, title: '时间'}
				,{title: '操作', width: 52, align:'center', fixed: 'right', toolbar: '#tableBar'}
			]]
			,page: true
			,limit: 20
			,height: 'full-100'
			,text: {none: '暂无相关数据'}
		});

		//监听搜索
		form.on('submit(LAY-search)', function(data){
			let field = data.field;
			//执行重载
			table.reload('LAY-table', {
				url: "{:url('getList')}"
				,where: field
				,page: {
					curr: 1 //重新从第 1 页开始
				}
			});
		});

		//表格内操作按钮
		table.on('tool(LAY-table)', function(obj){
			let data = obj.data;
			if(obj.event === 'edit'){
				layer.open({
					type: 2
					,title: '操作发票'
					,content: "{:url('edit')}?id=" + data.id
					,maxmin: true
					,area: ['750px', '90%']
					,btn: ['确定', '取消']
					,yes: function(index, layero){
						let iframeWindow = window['layui-layer-iframe'+ index];
						let submitID = 'LAY-submit';
						let submit = layero.find('iframe').contents().find('#'+ submitID);

						//监听提交
						iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
							let field = data.field; //获取提交的字段

							//提交 Ajax 成功后，静态更新表格中的数据
							$.ajax({
								type:"post",
								url:"{:url('save')}",
								data: {
									m_state:field.m_state,
									m_tax_no:field.m_tax_no,
									m_pic:field.m_pic,
									m_reason:field.m_reason,
									m_id:field.m_id
								},
								dataType:"json",
								beforeSend: function () {
									layer.load(1);
								},
								success:function(res){
									layer.close(layer.index);
									if(res.code == 0){
										layer.closeAll();
										table.reload('LAY-table',{where: {time:new Date()}}); //数据刷新
									}
									else {
										layer.msg(res.msg);
									}
								}
							});

						});

						submit.trigger('click');
					}
				});
			}
		});

	});
</script>